﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/User")]
    public class UserController : ApiController
    {
        B_User b;
        public UserController()
        {
            b = new B_User();
        }
        /// <summary>
        /// 获取到用户的基本信息（有分页）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUserData")]
        public IHttpActionResult GetUserData(int page, int size)
        {
            var user = b.GetAll();
            var uservip = new B_UserVip().GetAll();
            var viplevel = new B_VipLevel().GetAll();
            //var usercloudata = new B_UserCloudData().GetAll();
            var data = from u in user
                       join uv in uservip
                       on u.VipID equals uv.VipID
                       join vl in viplevel
                       on uv.LevelID equals vl.LevelID
                       select new Common.Param_User
                       {
                           UserID = u.UserID,
                           UserName = u.UserName,
                           PassWord = u.PassWord,
                           NickName = u.NickName,
                           HeadImg = u.HeadImg,
                           CreateTime = u.CreateTime,
                           Tel = u.Tel,
                           Email = u.Email,
                           VipName = vl.VipName,
                           VipID = uv.VipID,
                           VInTime = uv.VInTime,
                           VOutTime = uv.VOutTime,
                           LevelID = vl.LevelID,
                       };
            return Ok(new Result<List<Param_User>>()
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }
        /// <summary>
        /// 获取到用户的基本信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetUserData")]
        public IHttpActionResult GetUserData()
        {
            var user = b.GetAll();
            var uservip = new B_UserVip().GetAll();
            //var usercloudata = new B_UserCloudData().GetAll();
            var data = from u in user
                       join uv in uservip
                       on u.VipID equals uv.VipID
                       //join ucv in usercloudata
                       //on u.UCDID equals ucv.UCDID
                       select new Common.Param_User
                       {
                           UserID = u.UserID,
                           UserName = u.UserName,
                           PassWord = u.PassWord,
                           NickName = u.NickName,
                           HeadImg = u.HeadImg,
                           CreateTime = u.CreateTime,
                           Tel = u.Tel,
                           Email = u.Email,
                           VipName = "11",
                       };
            return Ok(new Result<List<Param_User>>()
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 只获取用户列表单表（有分页）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetData")]
        public IHttpActionResult GetData(int page, int size)
        {
            var data = from u in b.GetAll()
                       select new User
                       {
                           UserID = u.UserID,
                           UserName = u.UserName,
                           PassWord = u.PassWord,
                           NickName = u.NickName,
                           HeadImg = u.HeadImg,
                           CreateTime = u.CreateTime,
                           Tel = u.Tel,
                           TokenID = u.TokenID,
                           Email = u.Email
                       };
            return Ok(new Result<List<User>>
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }

        /// <summary>
        /// 只获取用户列表单表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetData")]
        public IHttpActionResult GetAll()
        {
            var data = from u in b.GetAll()
                       select new User
                       {
                           //补User的ID
                           UserID = u.UserID,

                           UserName = u.UserName,
                           PassWord = u.PassWord,
                           NickName = u.NickName,
                           HeadImg = u.HeadImg,
                           CreateTime = u.CreateTime,
                           Tel = u.Tel,
                           TokenID = u.TokenID,
                           Email = u.Email
                       };
            return Ok(new Result<List<User>> { Data = data.ToList() });
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(User user)
        {
            if (b.GetAll().Count(x => x.UserName == user.UserName) == 0)
            {
                b.Add(new User
                {
                    UserName = user.UserName,
                    PassWord = user.PassWord,
                    NickName = user.NickName,
                    HeadImg = user.HeadImg,
                    CreateTime = user.CreateTime,
                    Tel = user.Tel,
                    TokenID = user.TokenID,
                    Email = user.Email
                });
                return Ok(new Result<User> { });
            }
            else
            {
                return Ok(new Result<object>
                {
                    Code = 403,
                    Msg = "用户已存在"
                });
            }
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                b.Delete(id);
                return Ok(new Result<object>
                {
                    Code = 200,
                    Msg = "删除成功"
                });
            }
            catch (Exception)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = "该用户已删除"
                });
            }
        }

        /// <summary>
        /// 根据用户id查找用户详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Find/{id}")]
        public IHttpActionResult Find(int id)
        {
            try
            {
                if (b.FindById(id) == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "数据丢了"
                    });
                }
                var user = new B_User();
                var uservip = new B_UserVip();
                var viplevel = new B_VipLevel();
                var data = from u in user.GetAll()
                           join uv in uservip.GetAll()
                           on u.VipID equals uv.VipID
                           join vl in viplevel.GetAll()
                           on uv.LevelID equals vl.LevelID
                           where u.UserID == id
                           select new Common.Param_User
                           {
                               UserID = u.UserID,
                               UserName = u.UserName,
                               PassWord = u.PassWord,
                               NickName = u.NickName,
                               HeadImg = u.HeadImg,
                               CreateTime = u.CreateTime,
                               Tel = u.Tel,
                               TokenID = u.TokenID,
                               Email = u.Email,
                               VipID = u.VipID,
                               VipName = vl.VipName,
                               VInTime = uv.VInTime,
                               VOutTime = uv.VOutTime,
                               LevelID = uv.LevelID,
                               VStorageNum=vl.VStorageNum
                           };
                return Ok(new Result<Param_User>()
                {
                    Code = 200,
                    Data = data.FirstOrDefault()
                });
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }

        /// <summary>
        /// 更新用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, User user)
        {
            try
            {
                new B_User().Update(id, new User
                {
                    UserName = user.UserName,
                    PassWord = user.PassWord,
                    NickName = user.NickName,
                    HeadImg = user.HeadImg,
                    CreateTime = user.CreateTime,
                    Tel = user.Tel,
                    TokenID = user.TokenID,
                    Email = user.Email
                });
                return Ok(new Result<object>());
            }
            catch (Exception)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = "更新失败"
                });
            }
        }
    }
}
